const amqp = require('amqplib');
const express = require('express');
const app = express();

const RABBITMQ_URL = 'amqp://116.62.47.169:5672'; // RabbitMQ 服务器地址
const QUEUE_NAME = 'fanout_test_queue'; // 队列名称

app.get('/get-message', async (req, res) => {
  try {
    const connection = await amqp.connect(RABBITMQ_URL);
    const channel = await connection.createChannel();

    await channel.assertQueue(QUEUE_NAME);
    
    const message = await channel.get(QUEUE_NAME, { noAck: true });

    if (message) {
      res.json({ message: message.content.toString() });
    } else {
      res.json({ message: 'No messages in queue' });
    }

    await channel.close();
    await connection.close();
  } catch (error) {
    console.error('Error receiving message from RabbitMQ:', error);
    res.status(500).send('Error receiving message');
  }
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});